/* 
 *
 * Author: Allan <qimingos_lsk@163.com>, (C) 2005-2007
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
 *
 */
#include <stdio.h>
#include <glib.h>
#include <string.h>
#include "include/Version.h"
#include "include/Defines.h"

int StrLeftFind(gchar* szFrom, int iFrom, gchar ch)
{
	gchar* pWork = szFrom + strlen(szFrom) - 1 - iFrom;
	while (pWork >= szFrom)
	{
		if (*pWork == ch)
			return (int)(pWork - szFrom);
		pWork--;
	}
	return -1;
}
void LyricDebug(const gchar * format, ...)
{
#ifdef LYRIC_DEBUG
	va_list args;
	gchar szBuf[512] =
	{ 0 };
	va_start(args, format);
	vsnprintf(szBuf, 511, format, args);
	va_end(args);
	fprintf(stderr, "%s(%s): %s\n", PLUGIN_NAME, VERSION, szBuf);
#endif // LYRIC_DEBUG
}

void LyricLog(const gchar * format, ...)
{
#ifdef LYRIC_LOG
	va_list args;
	gchar szBuf[512] =
	{ 0 };
	va_start(args, format);
	vsnprintf(szBuf, 511, format, args);
	va_end(args);
	char szLogFileName[512] =
	{ 0 };
	sprintf(szLogFileName, "%s/log.txt", theApp.m_szConfigPath);
	FILE * f = fopen(szLogFileName, "a+");
	fprintf(f, "%s(%s):%s\n", PLUGIN_NAME, VERSION, szBuf);

	fprintf(stderr, "%s(%s): %s\n", PLUGIN_NAME, VERSION, szBuf);
	fclose(f);
#endif
}


gchar * GetNameFromFileName(const gchar* lpszFileName, gchar * lpszName, gint nLen)
{
	if (!lpszName || nLen == 0)return NULL;
	lpszName[0] = 0;
	gchar * lpszDot = g_utf8_strrchr(lpszFileName, MAX_PATH, '.');
	if (lpszDot)
	{
		int nCpy = nLen - 1;
		if (nCpy > lpszDot - lpszFileName)nCpy = lpszDot - lpszFileName;
		memcpy(lpszName, lpszFileName, nCpy);
		lpszName[nCpy] = 0;
	}
	else
	{
		strncpy(lpszName, lpszFileName, nLen);
	}
	return lpszName;
}
